package com.hpf.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @datetime: 2020/10/4 21:03
 * @author: huangpengfei
 */
@Service
public class PaymentService {

    public String paymentOk(Integer id){
        return "线程池：" + Thread.currentThread().getName()+ " paymentOk,id: "+ id;
    }

    /**
     * 熔断器注解实现服务降级
     * @param id
     * @return
     */
    @HystrixCommand(fallbackMethod = "paymentTimeoutFallback",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String paymentTimeout(Integer id){
        int time = 3;
        int age = 10 / 0;
//        try {
////            TimeUnit.SECONDS.sleep(time);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        return "我是8001，线程池：" + Thread.currentThread().getName()+ "  paymentTimeout,id: "+ id + " 耗时：" + time;
    }

    /**
     * 兜底的方法
     * @param id
     * @return
     */
    public String paymentTimeoutFallback(Integer id){
        return "8001系统发生异常："+ id;
    }
}
